package use;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Change {
    public void changeCustomer() throws IOException {
        Scanner sc = new Scanner(System.in);
        FileReader fr = new FileReader("information\\customer.txt");
        BufferedReader bfr = new BufferedReader(fr);
        FileWriter fw2 = new FileWriter("information\\customer.txt", true);
        List<String> list = new ArrayList<>();
        String str;
        String name;
        System.out.println("请输入用户名");
        name = sc.next();
        while ((str = bfr.readLine()) != null) {
            list.add(str);//在集合中一行一行接收文件中所有信息
            String[] sr = str.split(",");
            if (name.equals(sr[0])) {
                list.remove(str);//在集合中删除文件中的修改行
            }
        }
        FileWriter fw1 = new FileWriter("information\\customer.txt");
        System.out.println("请输入新的密码：");
        String newPassword = sc.next();
        String newUser = name + "," + newPassword;
        fw1.write(newUser);
        for (String user : list) {
            fw2.write("\r\n");
            fw2.write(user);
        }
        fw2.write("\r\n");
        fw1.close();
        fw2.close();
    }

    public void changePersonnel() throws IOException {
        Scanner sc = new Scanner(System.in);
        FileReader fr = new FileReader("information\\personnel.txt");
        BufferedReader bfr = new BufferedReader(fr);
        List<String> list = new ArrayList<>();
        String str;
        String[] sr = new String[3];
        String st = "0";
        System.out.println("请输入管理员姓名:");
        String name = sc.next();
        while ((str = bfr.readLine()) != null) {
            list.add(str);
            sr = str.split(",");

            if (name.equals(sr[0])) {
                list.remove(str);
                System.out.println("请选择:" + "\n" + "1、修改密码" + " " + "2、修改电话号码");
                String change = sc.next();
                switch (change) {
                    case "1":
                        System.out.println("请输入新的密码:");
                        sr[1] = sc.next();
                        break;
                    case "2":
                        System.out.println("请输入新的手机号:");
                        sr[2] = sc.next();
                        break;
                    default:
                        System.out.println("功能号不存在！请正确输入功能号！");
                        break;
                }
                st = sr[0] + "," + sr[1] + "," + sr[2];
            }
        }

        FileWriter fw1 = new FileWriter("information\\personnel.txt");
        fw1.write(st);
        FileWriter fw2 = new FileWriter("information\\personnel.txt", true);
        for (String si : list) {
            fw2.write("\r\n");
            fw2.write(si);
        }
        fw1.close();
        fw2.close();
        return;
    }

    public void deleteCustomer1() throws IOException {
        FileReader fr1 = new FileReader("information\\customer.txt");
        BufferedReader bfr1 = new BufferedReader(fr1);
        List<String> list1 = new LinkedList<>();
        String si;
        while ((si = bfr1.readLine()) != null) {
            list1.add(si);
        }
        for (String sg : list1) {
            System.out.println(sg);
        }
        bfr1.close();
        Scanner sc = new Scanner(System.in);
        FileReader fr = new FileReader("information\\customer.txt");
        BufferedReader bfr = new BufferedReader(fr);
        List<String> list = new ArrayList<>();
        String str;
        System.out.println("请输入要删除信息的顾客姓名:");
        String name = sc.next();
        while ((str = bfr.readLine()) != null) {
            list.add(str);
            String[] data = str.split(",");
            if (name.equals(data[0])) {
                list.remove(str);
            }
            FileWriter fw = new FileWriter("information\\customer.txt");
            for (String st : list) {
                fw.write(st);
                fw.write("\r\n");
            }
            fw.close();
        }
    }
    public void deleteCustomer2()throws IOException{
        Scanner sc = new Scanner(System.in);
        FileReader fr = new FileReader("information\\customer.txt");
        BufferedReader bfr = new BufferedReader(fr);
        List<String> list = new ArrayList<>();
        String str;
        System.out.println("请输入要删除信息的顾客姓名:");
        String name = sc.next();
        while ((str = bfr.readLine()) != null) {
            list.add(str);
            String[] data = str.split(",");
            if (name.equals(data[0])) {
                list.remove(str);
            }
            FileWriter fw = new FileWriter("information\\customer.txt");
            for (String st : list) {
                fw.write(st);
                fw.write("\r\n");
            }
            fw.close();
        }
    }
}
